package action;

import dao.OrderDao;
import dao.OrderItemDao;
import po.User;
import vo.OrderItemVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/user/action/ShowOrderServlet")
public class ShowOrderServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User)req.getSession().getAttribute("user");
        int pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
        int start = Integer.parseInt(req.getParameter("start"));
        int length = Integer.parseInt(req.getParameter("length"));
        OrderItemDao orderItemDao = new OrderItemDao();
        int recordsTotal = orderItemDao.selectCountByUser(user);
        //获取session中的user
        //调用dao层方法
        OrderItemVo orderItemVo = new OrderItemVo();
        OrderDao orderDao = new OrderDao();
        ArrayList<OrderItemVo> orderItemVos = new ArrayList<OrderItemVo>();
        orderItemVos = orderDao.findAllByUser(user.getPhone(),start,length);
        //绑定数据
        req.setAttribute("pageCount", recordsTotal>0?recordsTotal/length+1:0);
        req.setAttribute("current", pageIndex);
        req.setAttribute("length", length);
        req.setAttribute("orderItemVos",orderItemVos);
        //转发
        req.getRequestDispatcher("../page/order.jsp").forward(req,resp);
    }
}
